Ext.define('cloud.controller.Article', {
    extend: 'Ext.app.Controller',
    config: {
        file: '',
        urldata: '',
        filedata: [],
        refs: {
            article: 'article',
            articleAdd: 'articleAdd',
            article_add: 'article #article_add',
            article_search: 'article #article_search',
            article_submit: 'articleAdd #article_submit',
            article_ccid: 'article #article_ccid',
            articleEdit: 'articleEdit',
            article_edit: 'articleEdit #article_edit',
            article_delet: 'articleEdit #article_delet',
            article_upload: 'articleAdd #article_upload'
        },
        control: {
            article_upload: {
                loadsuccess: function (dataurl, m, e, file) {
                    var img_name = 'article_loadedImage';
                    file['display'] = 1;
                    this.setUrldata = dataurl;
                    this.upload(dataurl, m, e, file, img_name);
                }
            },

            article_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: "loadmask"});
                    var article = this.getArticle();
                    var articleadd = this.getArticleAdd();
                    if (articleadd == null) {
                        articleadd = Ext.create('cloud.view.article.ArticleAdd');
                    }
                    article.push(articleadd)
                    Ext.Viewport.unmask();
                }
            },

            'article list': {
                itemtap: function (list, index, element, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var articleedit = this.getArticleEdit()||Ext.create('cloud.view.article.ArticleEdit', {title: record.get('name')});
                    var form = this.getArticle().push(articleedit);
                    form.setRecord(record);
                    articleedit.initeditor();
                    document.getElementById('imghead').src = record.get('picurl')
                    Ext.Viewport.unmask();
                }
            },

            article_submit: {
                tap: function () {
                    var ccid=Ext.getCmp('article_add_ccid').getValue();
                    var name=Ext.getCmp('article_name_add').getValue();
                    var status=Ext.getCmp('article_status_add').getValue();
                    if (Ext.isEmpty(name)){
                        Ext.Msg.alert('请输入资讯名称');
                    }
                    //编辑器的文本内容
                    var content=document.getElementsByClassName('nicEdit-main')[0].innerHTML
                    if (Ext.isEmpty(content)){
                        Ext.Msg.alert('请输入详细内容');
                    }
                    var file=document.getElementById('article_img').files[0];
                    var backthis = this;
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    //添加没有图片的咨询
                    if (file == null ||file == undefined) {
                        Ext.Ajax.request({
                            url: GLOBAL.api_url + 'article_add',
                            scope: this,
                            method: 'POST',
                            params: {
                                ccid: ccid,
                                content: content,
                                name: name,
                                picurl: '',
                                status:status
                            },
                            success: function (response) {
                                Ext.Viewport.unmask();
                                var ccid = Ext.getCmp('article_ccid').getValue();
                                var store = Ext.getStore('ArticleColumnFindStore');
                                store.load({params: {ccid: ccid}});
                                var artic = this.getArticle();
                                artic.pop()
                            },
                            failure: function () {//请求失败时执行操作
                                Ext.Viewport.unmask();
                                Ext.Msg.alert('请求失败', '服务器维护中....');
                            }
                        });
                    }
                    //有资讯图片的资讯
                    else{
                        var paramObj={
                            ccid: ccid,
                            content: content,
                            name: name,
                            picurl: '',
                            status:status
                        };
                        this.addOrUpdateImgFn(file,paramObj,"_add");
                    }
                }
            },

            article_edit: {
                tap: function () {
                    var caid=Ext.getCmp('article_edit_caid').getValue();
                    var ccid=Ext.getCmp('article_edit_ccid').getValue();
                    var name=Ext.getCmp('article_edit_name').getValue();
                    var statu=Ext.getCmp('article_edit_status').getValue();
                    var content=document.getElementsByClassName('nicEdit-main')[0].innerHTML
                    if (Ext.isEmpty(name)){
                        Ext.Msg.alert('请输入广告名称');
                    }
                    if (Ext.isEmpty(content)){
                        Ext.Msg.alert('请输入广告内容');
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var backthis = this;
                    var file = document.getElementById('article_img').files[0];
                    //没有改图片更新资讯
                    if (file == null || file == undefined) {
                        Ext.Ajax.request({
                            url: GLOBAL.api_url + 'article_update',
                            scope: this,
                            method: 'POST',
                            params: {
                                caid:caid,
                                ccid: ccid,
                                content: content,
                                name: name,
                                picurl: '',
                                status:statu
                            },
                            success: function (response) {
                                Ext.Viewport.unmask();
                                var returnText = Ext.decode(response.responseText);
                                if (returnText.Variables.Result.code == 0) {
                                    var ccid = Ext.getCmp('article_ccid').getValue();
                                    var store = Ext.getStore('ArticleColumnFindStore');
                                    store.load({params: {ccid: ccid}});
                                    var artic = this.getArticle();
                                    artic.pop()

                                }
                                Ext.Msg.alert(returnText.Variables.Message.messagestr)
                            },
                            failure: function () {//请求失败时执行操作
                                Ext.Viewport.unmask();
                                Ext.Msg.alert('请求失败', '服务器维护中....');
                            }
                        });
                    }
                    //修改图片
                    else {
                        var paramObj={
                            caid:caid,
                            ccid: ccid,
                            content: content,
                            name: name,
                            picurl: '',
                            status:statu
                        };
                        this.addOrUpdateImgFn(file,paramObj,"_update");
                    }
                }
            },

            article_delet: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getArticleEdit().submit({
                        url: GLOBAL.api_url + 'article_delete',
                        scope: this,
                        method: 'POST',
                        params: {
                            caid: Ext.getCmp('article_edit_caid').getValue()
                        },
                        success: function () {
                            Ext.Viewport.unmask();
                            var ccid = Ext.getCmp('article_ccid').getValue()
                            var store = Ext.getStore('ArticleColumnFindStore');
                            store.load({params: {ccid: ccid}});
                            var article = this.getArticle();
                            article.pop()
                        },
                        failure: this.actionfailure
                    })
                }
            },

            article_search: {
                change: function () {
                    var Store = Ext.getStore('ArticleColumnFindStore');
                    var value = this.getArticle_search().getValue();
                    Store.clearFilter();
                    if (value != "") {
                        Store.filter('name', value, true);
                    }
                }
            }
        }
    },
    addOrUpdateImgFn:function(file,paramObj,opt){
        var backthis=this;
        var formdata = new FormData();
        formdata.append("Filedata", file);
        var http = new XMLHttpRequest();
        if (http.upload && http.upload.addEventListener) {
            http.onreadystatechange = function (e) {
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        var returnText = Ext.decode(this.responseText);
                        if (returnText.Variables.Result.code == 0) {
                            var picurl = returnText.Variables.data.attachdir;
                            paramObj.picurl=picurl;
                            Ext.Ajax.request({
                                url: GLOBAL.api_url + 'article'+opt,
                                scope: this,
                                method: 'POST',
                                params: paramObj,
                                success: function (response) {
                                    Ext.Viewport.unmask();
                                    var returnText = Ext.decode(response.responseText);
                                    if (returnText.Variables.Result.code == 0) {
                                        var ccid = Ext.getCmp('article'+opt+'_ccid').getValue();
                                        var store = Ext.getStore('ArticleColumnFindStore');
                                        store.load({params: {ccid: ccid}});
                                        var artic = backthis.getArticle();
                                        artic.pop()
                                    }
                                    Ext.Msg.alert(returnText.Variables.Message.messagestr)
                                },
                                failure: function () {//请求失败时执行操作
                                    Ext.Viewport.unmask();
                                    Ext.Msg.alert('请求失败', '服务器维护中....');
                                }
                            });
                        }
                    }
                }
            };
            http.upload.onerror = this.actionfailure;
        }
        http.open('POST', GLOBAL.api_url + 'attachment_upload&updatatype=article');
        http.send(formdata);
    },
    actionfailure: function () {//请求失败时执行操作
        Ext.Viewport.unmask();
        Ext.Msg.alert('请求失败', '服务器维护中....');
    }


})